<template>
  <section>
      <el-row :gutter="20">
        <el-col :span="16">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                <el-form-item label="选择用户：" prop="memberId">
                    {{ form.memberMobile }}
                    <el-button type="primary" size="mini" @click="select()">选择</el-button>
                </el-form-item>
                <el-form-item label="订购类型：" prop="sellerType">
                    <el-radio-group v-model="form.sellerType">
                        <el-radio :label="1">合伙人</el-radio>
                        <el-radio :label="2">联盟商</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="实付价：" prop="actualAmount">
                    <el-input type="number" size="mini" style="width:200px;" v-model="form.actualAmount" placeholder="" />
                </el-form-item>
                <el-form-item label="订购天数：" prop="days">
                    <el-input type="number" size="mini" style="width:200px;" v-model="form.days" placeholder="" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="mini" @click="next()" :loading="saveLoading">确定添加</el-button>
                </el-form-item>
            </el-form>
        </el-col>
      </el-row>
    <el-dialog title="选择用户" :visible.sync="dialogMemberVisible" :close-on-click-modal="false" width="900px">
        <member-list :formData.sync="memberForm" @okSelect="okSelectMember" />
        <div slot="footer" class="dialog-footer">
            <el-button size="mini" type="primary" @click="dialogMemberVisible=false">关闭</el-button>
        </div>
    </el-dialog>
  </section>
</template>

<script>
    import {
        seller_order_create
    } from '../../api/seller';
    export default {
        components: {
            'member-list': () => import('../../components/Member/index'),
        },
    data() {
        return {
            dialogMemberVisible: false,
            memberForm: {
                is_select: true
            },
            form: {
                memberId: undefined,
                memberMobile: undefined,
                sellerType: undefined,
                actualAmount: undefined,
                // days: 365,
            },
            saveLoading: false,
            rules: {
                memberId: [
                    {
                        required: true,
                        message: '请选择订购用户',
                        trigger: 'blur'
                    }
                ],
                sellerType: [
                    {
                        required: true,
                        message: '请选择小店类型',
                        trigger: 'blur'
                    },
                ],
                days: [
                    {
                        required: true,
                        message: '请输入订购天数',
                        trigger: 'blur'
                    },
                ],
            }
        }
    },
    mounted() {
    },
    methods: {
        select() {
            this.dialogMemberVisible = true;
        },
        next() {
            const set = this.$refs;
            set["form"].validate(valid => {
                if (valid) {
                    this.$confirm('确定订购?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    })
                        .then(() => {
                            this.saveLoading = true;
                            seller_order_create(this.form).then((res)=>{
                                this.saveLoading = false;
                                this.$message.success("订单创建成功");

                                this.$router.push("/seller/orders");
                            }).catch((err)=>{
                                this.saveLoading = false;
                            })

                        }).catch(()=>{});
                } else {
                    return false;
                }
            });
        },
        onSelect(item) {
            this.form.productId = item.id;
            this.form.skuId = undefined;
            this.dialogSelectVisible = false;

            this.getDetail();
        },
        getDetail() {
            product_detail(this.form.productId).then(response => {
                let form = response.data;
                this.productDetail = form;
            }).catch(()=>{
            });
        },
        okSelectMember(item) {
            // if(item.roleId != 0) {
            //     this.$message.error("只能选择鲸粉");
            // } else {
                this.form.memberId = item.id;
                this.form.memberMobile = item.mobile;
                this.dialogMemberVisible = false;
            // }
            // console.log(item)
        }
    }
    };
</script>
